<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>档案借阅</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <jsp:include page="/inc/head.jsp" flush="true"/>
    <jsp:include page="/inc/footer.jsp" flush="true"/>
    <!-- picker -->
    <script src="${basePath}/js/My97DatePicker/WdatePicker.js"></script>
    <script src="${basePath }/js/tip.js"></script>
    <style>
        .content .bootstrap-table .fixed-table-toolbar .icon-refresh {
            padding: 3px 0;
        }
        .content .bootstrap-table .fixed-table-toolbar .bs-bars, .fixed-table-toolbar .columns {
            margin-top: 0;
        }
    </style>
</head>
<body class="hold-transition skin-blue sidebar-mini" >
<!-- Main content -->
<section class="content">
    <div id="toolbar" class="btn-group">
        <shiro:hasPermission name="cms:borrowreturn:borrow">
        <button id="btn_borrow" type="button" class="btn btn-default" onclick="beSureButton()">
            <span class="glyphicon glyphicon-log-out" aria-hidden="true"></span>确认借出
        </button>
        </shiro:hasPermission>
        <shiro:hasPermission name="cms:borrowreturn:return">
        <button id="btn_return" type="button" class="btn btn-default" onclick="returnButton()">
            <span class="glyphicon glyphicon-log-in" aria-hidden="true"></span>确认归还
        </button>
        </shiro:hasPermission>
    </div>
    <table id="table"></table>
</section>
</body>
<script type="text/javascript">
    var ID;
    var $table = $('#table');
    $(document).ready(function(){
        $table.bootstrapTable({
            url : "${basePath}/pcwindow/borReturnList", // 获取数据的地址
            striped: true,
            search: false,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns:2,
            clickToSelect: true,
            detailView: false,
            detailFormatter: '',
            pagination: true,
            pageSize: 50,
            pageList:[50,100],
            sidePagination: 'server',
            silentSort: false,
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'ID',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns :
                [
                    {field: 'ck', checkbox: true},
                    {field: 'REGISTID', visible:false},
                    {field: 'REGISTERNUM', title: '登记号', align: 'center'},
                    {field: 'EXTEND1', title: '档案档号', align: 'center'},
                    {field: 'TITLE', title: '借阅文件', align: 'center'},
                    {field: 'USERNAME', title: '借阅人', align: 'center'},
                    {field: 'PHONE', title: '电话号', align: 'center'},
                    {field: 'DEPTNAME', title: '借阅人部门', align: 'center'},
                    {field: 'PLANRETURNTIME', title: '截止时间', align: 'center',formatter:'timeFormatter'},
                    {field: '', title: '逾期状况', align: 'center',formatter:'overdueFormatter'},
                    {field: '', title: '操作', align: 'center',formatter:'optionFormatter'}
                ],
            onLoadSuccess:function(data){
                //无数据隐藏分页
                if(data && data.total == 0){
                    $('.fixed-table-pagination').hide()
                }
            }
        });
        $table.bootstrapTable('resetView',{height:setHeight()});

    })

    /* 初始化时间格式 */
    function timeFormatter(value,row,index){
        return longToDate(value);
    }

    /* 初始化逾期格式 */
    function overdueFormatter(value,row,index){
        var nowDateLong = new Date().getTime();
        var planReturnTime = row.PLANRETURNTIME;
        var OVERDUESTATUS = nowDateLong-planReturnTime;
        var num=Math.round(OVERDUESTATUS/86400000);
        if(num<=-3){
            return '<span  class="label label-success">未逾期</span>';
        }else if(num<=1&num>=-2){
            return '<span class="label label-warning">即将逾期</span>';
        }else if(num>1){
            return '<span class="label label-danger">已逾期</span>';
        }else{
            return ""
        }
    }

    /* 初始化操作按钮 */
    function optionFormatter(value, row, index){
        if(row.BORSTATUS==0){
            return [
                '<shiro:hasPermission name="cms:borrowreturn:borrow"><a class="update" href="javascript:;" onclick="beSureButton(' + row.REGISTID +');" data-toggle="tooltip"  title="确认借出" ><i class="glyphicon glyphicon-log-out"></i></i></a></shiro:hasPermission>'
            ].join('');
        }else{
            return [
                '<shiro:hasPermission name="cms:borrowreturn:return"><a class="update" href="javascript:;" onclick="returnButton(' + row.REGISTID +');" data-toggle="tooltip"  title="确认归还" ><i class="glyphicon glyphicon-log-in"></i></i></a></shiro:hasPermission>'
            ].join('');
        }
    }

    /**
     * 确认借出按钮
     */
    function beSureButton(id){
        var rows = $table.bootstrapTable('getSelections');
        var ids = [];
        if(id==null||typeof(id)=="undefined"){
            if (rows.length == 0) {
                confirmEvent('请至少选择一条记录!');
                return;
            }else{
                for(var index in rows){
                    ids.push(rows[index].REGISTID);
                }
            }
        }else{
            ids.push(id);
        }
        Ewin.confirm({ message: "请确认被选中的档案是否借出，一经确认无法更改！" }).on(function (e) {
            if(!e){
                return;
            }
            $.ajax({
                url: BASE_PATH + "/pcwindow/beSureBorrow",
                data:{ids:ids.join("-")},
                type:"post",
                success:function(data){
                    if(data.code==1){
                        confirmEvent(data.message);
                        $table.bootstrapTable('refresh');
                    }else{
                        confirmEvent(data.message);
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    confirmEvent('系统开小差了，请稍后再试!');
                }

            })
        })
    }

    /**
     * 确认归还按钮
     * @param id
     */
    function returnButton(id){
        var rows = $table.bootstrapTable('getSelections');
        var ids = [];
        if(id==null||typeof(id)=="undefined"){
            if (rows.length == 0) {
                confirmEvent('请至少选择一条记录!');
                return;
            }else{
                for(var index in rows){
                    ids.push(rows[index].REGISTID);
                }
            }
        }else{
            ids.push(id);
        }
        var fileType=$("#fileType").val();
        Ewin.confirm({ message: "请确认被选中的档案是否归还，一经确认无法更改！" }).on(function (e) {
            if(!e){
                return;
            }
            $.ajax({
                url: BASE_PATH + "/pcwindow/makeSureReturn",
                data:{ids:ids.join("-"),fileType:fileType},
                type:"post",
                success:function(data){
                    if(data.code==1){
                        confirmEvent(data.message);
                        $table.bootstrapTable('refresh');
                    }else{
                        confirmEvent(data.message);
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    confirmEvent('系统开小差了，请稍后再试!');
                }
            })
        })
    }
    /**
     * 表格高度自适应
     * @return
     * winTop 浏览器视口高度
     * header 顶部通栏
     * breadcrumb 面包屑
     * mainFooter 底部
     */
    function setHeight(){
        var winTop = $(window.top).height(),
            header = window.top.document.querySelector('.main-header').offsetHeight,
            breadcrumb =  window.top.document.querySelector('.breadcrumb').offsetHeight,
            mainFooter = window.top.document.querySelector('.main-footer').offsetHeight;
        return winTop-header-breadcrumb-mainFooter-10;
    }
</script>
</html>